package com.example.demo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.HttpRequest;
import com.example.demo.ResultResponse;
import com.example.demo.domain.*;
import com.example.demo.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yrforest
 * @version 1.0
 * @description: TODO
 * @date 2025/7/10 10:23
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户测试接口")
public class UserController {

    @Autowired
    UserService userService;

    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public ResultResponse<Boolean> userRegister(@RequestBody UserRegisterReq userRegisterReq) {
        boolean res = userService.register(userRegisterReq);
        return ResultResponse.success(res);
    }

    @PostMapping("/login")
    @Operation(description = "用户登录", summary = "用户登录")
    public ResultResponse userLogin(@RequestBody UserLoginReq userLoginReq) {
        LoginUserResp loginUser = userService.login(userLoginReq);
        return ResultResponse.success(loginUser);
    }

    @SaCheckLogin
    @PostMapping("/logout")
    @Operation(description = "用户注销", summary = "用户注销")
    public ResultResponse userLogin() {
        StpUtil.logout();
        return ResultResponse.success(null);
    }


    // 仅用户登录情况下可用
    @GetMapping("/current")
    @SaCheckLogin
    @Operation(summary = "获取当前用户信息")
    public ResultResponse current(HttpServletRequest httpServletRequest) {
//        LoginUserResp loginUser = (LoginUserResp) httpServletRequest.getSession().getAttribute("login_user");
        LoginUserResp loginUser = (LoginUserResp) StpUtil.getSession().get("login_user");
        return ResultResponse.success(loginUser);
    }


    @GetMapping("/search/{id}")
    @Operation(summary = "管理员查看指定用户")
    @SaCheckRole("admin")
    public ResultResponse searchUser(@PathVariable("id") Long id) {
        SearchUserResp searchUserResp = userService.search(id);
        return ResultResponse.success(searchUserResp);
    }


    @GetMapping("/remove/{id}")
    @Operation(summary = "管理员删除指定用户")
    @SaCheckRole("admin")
    public ResultResponse deleteUser(@PathVariable("id") Long id) {
        Boolean res = userService.delete(id);
        return ResultResponse.success(res);
    }

    @GetMapping("/update")
    @Operation(summary = "管理员更新指定用户")
    @SaCheckRole("admin")
    public ResultResponse deleteUser(@RequestBody UpdateUserReq updateUserReq) {
        Boolean res = userService.update(updateUserReq);
        return ResultResponse.success(res);
    }


//    @GetMapping("/{id}")
//    public String getUserInfo(@PathVariable("id") Long id) {
//        return "get userinfo success" + id;
//    }
//
//    @GetMapping("/query")
//    public String getUserbyQuery(Long id) {
//        return id + " query params success";
//    }


}
